#!/bin/bash

#  helper script: downloads dejavu sans mono
#
#	Copyright (C) 2021 Leah Rowe <info@minifree.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

tarurl="http://sourceforge.net/projects/dejavu/files/dejavu/2.37/dejavu-fonts-ttf-2.37.tar.bz2"
tarfilename="dejavu-fonts-ttf-2.37.tar.bz2"
dirname="dejavu-fonts-ttf-2.37"
tarchecksum="bafa39321021097432777f0825d700190c23f917d754a4504722cd8946716c22c083836294dab7f3ae7cf20af63c4d0944f3423bf4aa25dbca562d1f30e00654  ${tarfilename}"
ttffilename="DejaVuSansMono.ttf"
ttfchecksum="9c09b9d5bef34dd57f1dd9d98e7d92c1dd7892d22b4e7ca040a8130b3555aa19815b8a7c94b869ff436220e996a69847b46eaddf765c11569dffe9d72644ee38  ${ttffilename}"

checkttf() {
	dirpath="${1}"
	if [ -d "${dirpath}" ]; then
		if [ -f "${dirpath}/ttf/${ttffilename}" ]; then
			cd "${dirpath}/ttf/"
			if [ "$(sha512sum ${ttffilename})" = "${ttfchecksum}" ]; then
				cd ../../
				printf "REMARK: '%s/ttf/%s' checksum verification passed.\n" "${dirpath}" "${ttffilename}"
				return 0
			fi
			cd ../../
		fi
		printf "REMARK: '%s/ttf/%s' checksum verification FAILED.\n" "${dirpath}" "${ttffilename}"
	fi
	return 1
}

if [ -d "dejavusansmono/" ]; then
	checkttf "dejavusansmono" || rm -Rf "dejavusansmono/"
	if [ -d "dejavusansmono/" ]; then
		rm -f "${tarfilename}"
		rm -Rf "${dirname}/"
		printf "REMARK: download/dejavusansmono: DeJavu already downloaded and checksum passed.\n"
		exit 0
	else
		printf "REMARK: download/dejavusansmono: DeJavu font will be re-downloaded.\n"
	fi
fi

if [ -d "${dirname}/" ]; then
	checkttf "${dirname}" || rm -Rf "${dirname}/"
	if [ ! -d "${dirname}/" ]; then
		printf "REMARK: download/dejavusansmono: DeJavu font will be re-downloaded.\n"
	fi
fi

if [ ! -d "${dirname}/" ]; then
	if [ -f "${tarfilename}" ]; then
		if [ "$(sha512sum ${tarfilename})" != "${tarchecksum}" ]; then
			printf "REMARK: download/dejavusansmono: archive already downloaded, but checksum failed. Re-downloading\n"
			printf "Deleting archive\n"
			rm -f "${tarfilename}"
			wget ${tarurl}
		else
			printf "REMARK: download/dejavusansmono: archive already downloaded, and checksum passed. Skipping download\n"
		fi
	else
		wget ${tarurl}
	fi

	if [ ! -f "${tarfilename}" ]; then
		printf "ERROR: download/dejavusansmono: archive not downloaded. Exiting.\n"
		exit 1
	fi

	if [ "$(sha512sum ${tarfilename})" != "${tarchecksum}" ]; then
		printf "ERROR: download/dejavusansmono: archive has invalid checksum. Exiting\n"
		printf "Deleting archive.\n"
		rm -f "${tarfilename}"
		exit 1
	fi

	tar -xf "${tarfilename}" || touch osbmk_error
fi

if [ -f osbmk_error ]; then
	rm -f osbmk_error
	rm -Rf "dejavusansmono"
	rm -Rf "${dirname}"
	printf "ERROR: download/dejavusansmono: problem extracting archive.\n"
	exit 1
fi

mv "${dirname}" "dejavusansmono"
checkttf "dejavusansmono" || touch osbmk_error

if [ -f osbmk_error ]; then
	rm -f osbmk_error
	rm -Rf "dejavusansmono"
	rm -Rf "${dirname}"
	rm -f "${tarfilename}"
	printf "ERROR: download/dejavusansmono: TTF file got corrupted. Tar is cursed.\n"
	exit 1
fi

rm -Rf "${dirname}"
rm -Rf "${tarfilename}"
printf "REMARK: download/dejavusansmono: Dejavu Sans Mono font successfully downloaded.\n"
exit 0








